﻿using System;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(AIMove))]
public class AIItemDetector : AIBehaviour
{
    private GameObject lastTarget;
    public float needToHealEnergyPercent = 0.6f;
    public float needToHealHpPercent = 0.6f;
    public float triggerRadius = 2f;

    [CompilerGenerated]
    private static bool <FindNearbyItem`1>m__43<T>(Item x) where T: Item
    {
        return (x is T);
    }

    [CompilerGenerated]
    private int <FindNearbyItem`1>m__44<T>(Item a, Item b) where T: Item
    {
        return this.CompareDistance(a.transform.position, b.transform.position);
    }

    private bool CanPickup(Item item)
    {
        if (item == null)
        {
            return false;
        }
        if (item is Suit)
        {
            return false;
        }
        if ((item is HpIncreaser) && !this.isNeedToIncreaseHp)
        {
            return false;
        }
        if ((item is EnergyIncreaser) && !this.isNeedToIncreaseEnergy)
        {
            return false;
        }
        Weapon weapon = item as Weapon;
        if (weapon != null)
        {
            if (weapon.isBroken)
            {
                return false;
            }
            if ((base.inventory.weapon != null) && (base.inventory.secondWeapon != null))
            {
                return false;
            }
        }
        if ((Time.time - item.lastDropTime) < 0.1f)
        {
            return false;
        }
        return (((item != null) && (item.user == null)) && (item.gameObject.layer == GameLayers.Item.index));
    }

    private int CompareDistance(Vector3 a, Vector3 b)
    {
        Vector3 vector = a - base.position;
        Vector3 vector2 = b - base.position;
        return vector.magnitude.CompareTo(vector2.magnitude);
    }

    private Item FindNearbyItem<T>() where T: Item
    {
        Item[] array = Item.all.Where<Item>(new Func<Item, bool>(this.CanPickup)).Where<Item>(new Func<Item, bool>(AIItemDetector.<FindNearbyItem`1>m__43<T>)).ToArray<Item>();
        Array.Sort<Item>(array, new Comparison<Item>(this.<FindNearbyItem`1>m__44<T>));
        return array.FirstOrDefault<Item>();
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    private void SetTargetToNearbyItem<T>() where T: Item
    {
        if (((Time.frameCount % 30) == 0) && (base.targetDetector != null))
        {
            Item item = this.FindNearbyItem<T>();
            if ((item != null) && this.CanPickup(item))
            {
                if (Vector3.Distance(base.transform.position, item.transform.position) < 1f)
                {
                    base.inventory.Pickup(item);
                }
                else
                {
                    base.targetDetector.target = item.gameObject;
                }
            }
        }
    }

    protected override void Start()
    {
        base.Start();
        if (base.inventory != null)
        {
            SphereCollider comp = this.CreateChildGameObject<SphereCollider>("AIDetectItemTrigger");
            comp.isTrigger = true;
            comp.radius = this.triggerRadius;
            SphereCollider collider2 = this.CreateChildGameObject<SphereCollider>("AIPickupItemTrigger");
            collider2.isTrigger = true;
            collider2.radius = 1f;
            comp.AddComponent<TriggerEventListener>().onTriggerEnter = delegate (Collider co) {
                if ((this.target != null) && (this.target.layer != GameLayers.Item.index))
                {
                    this.lastTarget = this.target;
                }
                Item item = co.GetComponentInRoot<Item>();
                if (this.CanPickup(item) && (base.targetDetector != null))
                {
                    base.targetDetector.target = item.gameObject;
                }
            };
            collider2.AddComponent<TriggerEventListener>().onTriggerEnter = delegate (Collider co) {
                Item componentInRoot = co.GetComponentInRoot<Item>();
                if ((componentInRoot != null) && this.CanPickup(componentInRoot))
                {
                    base.inventory.Pickup(componentInRoot);
                    if (base.targetDetector != null)
                    {
                        base.targetDetector.target = this.lastTarget;
                    }
                }
            };
        }
    }

    private void UNetVersion()
    {
    }

    private void Update()
    {
        if (base.inventory.weapon == null)
        {
            this.SetTargetToNearbyItem<Weapon>();
        }
        else if (this.isNeedToIncreaseHp)
        {
            this.SetTargetToNearbyItem<HpIncreaser>();
        }
    }

    private bool isNeedToIncreaseEnergy
    {
        get
        {
            return (((base.character != null) && !base.character.isInfinteEnergy) && ((base.character.energy / base.character.maxEnergy) <= this.needToHealEnergyPercent));
        }
    }

    private bool isNeedToIncreaseHp
    {
        get
        {
            return ((base.life != null) && ((base.life.hp / base.life.maxHp) <= this.needToHealHpPercent));
        }
    }

    private GameObject target
    {
        get
        {
            return ((base.targetDetector == null) ? null : base.targetDetector.target);
        }
    }
}

